package com.mani.geotag;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

public class ProgressDialog extends Dialog{

	final int TWITTER_SHARING = 1;
	final int FACEBOOK_SHARING = 2;
	final int SMS_SHARING = 3;
	
	private static int PORTRAIT_ORIENTATION = 4;
	private static int LANDSCAPE_ORIENTATION =5;
	final String FONT_TTF = "bandless.ttf";
	Context mContext;
	TextView mTwitter;
	TextView mFacebook;
	TextView mSms;
	TextView mDone;
	ImageView mTwitter_spinner;
	ImageView mFacebook_spinner;
	ImageView mSms_spinner;
	int mHeight;
	int mWidth;
	DisplayMetrics metrics = new DisplayMetrics();
	static Typeface mFont;	

	
	ProgressDialog(Context context) {
		super(context,R.style.contactsdialog);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.progressdialog);

		mContext = context;
		mTwitter = (TextView) findViewById(R.id.twitter);
		mFacebook = (TextView) findViewById(R.id.facebook);
		mSms = (TextView) findViewById(R.id.sms);
		mDone = (TextView)findViewById(R.id.done);
		mTwitter.setTypeface(getTypeface(context,FONT_TTF));
		mFacebook.setTypeface(getTypeface(context,FONT_TTF));
		mSms.setTypeface(getTypeface(context,FONT_TTF));
		mDone.setTypeface(getTypeface(context,FONT_TTF));
		mDone.setVisibility(View.INVISIBLE);
		mTwitter_spinner = (ImageView) findViewById(R.id.twitter_spinner);
		mFacebook_spinner = (ImageView) findViewById(R.id.facebook_spinner);
		mSms_spinner = (ImageView) findViewById(R.id.sms_spinner);
		
		mDone.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ProgressDialog.this.dismiss();
			}
		});
		Animation mAnimation = AnimationUtils.loadAnimation(mContext, R.anim.spinner);
		mTwitter_spinner.startAnimation(mAnimation);
		mFacebook_spinner.startAnimation(mAnimation);
		mSms_spinner.startAnimation(mAnimation);	

		   WindowManager.LayoutParams params = getWindow().getAttributes();  

	        if(getscreenOrientation() == PORTRAIT_ORIENTATION) {
		        params.height = (mHeight*40)/100;
		        params.width = (mWidth*90)/100;
	        } else {
		        params.height = (mHeight*50)/100;
		        params.width = (mWidth*80)/100;
	        }

	        this.getWindow().setAttributes(params);	  
	        this.setCancelable(false);
	}
	boolean mTwitter_set;
	boolean mFacebook_set;
	boolean mSms_set;
	public void setStates(boolean twitter, boolean facebook, boolean sms) {
		
		mTwitter_set = twitter;
		mFacebook_set = facebook;
		mSms_set = sms;
		
		if(!twitter) 
		{
			mTwitter_spinner.clearAnimation();
			mTwitter_spinner.setImageResource(R.drawable.cross);
		}
		if(!facebook)
		{
			mFacebook_spinner.clearAnimation();
			mFacebook_spinner.setImageResource(R.drawable.cross);
		}
		if(!sms)
		{
			mSms_spinner.clearAnimation();
			mSms_spinner.setImageResource(R.drawable.cross);
		}
	}
	
	public static Typeface getTypeface(Context context, String typeface) {
	    if (mFont == null) {
	        mFont = Typeface.createFromAsset(context.getAssets(), typeface);
	    }
	    return mFont;
	}
	
	public int getscreenOrientation() {
		((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE))
				.getDefaultDisplay().getMetrics(metrics);
		mWidth = metrics.widthPixels;
		mHeight = metrics.heightPixels;

		if (metrics.heightPixels > metrics.widthPixels)
			return PORTRAIT_ORIENTATION;
		else
			return LANDSCAPE_ORIENTATION;
	}
	
	public void updateTwitterSuccess() {
		mTwitter_spinner.clearAnimation();
		mTwitter_spinner.setImageResource(R.drawable.tick);
		mTwitter.setText(mContext.getString(R.string.twitter_success));
		if(mFacebook_set == false && mSms_set == false) {
			mDone.setVisibility(View.VISIBLE);
		}
	}
	
	public void updateFaceBookSuccess() {
		mFacebook_spinner.clearAnimation();
		mFacebook_spinner.setImageResource(R.drawable.tick);
		mFacebook.setText(mContext.getString(R.string.facebook_success));
		if(mSms_set == false) {
			mDone.setVisibility(View.VISIBLE);
			this.setCancelable(true);
		}
	}
	
	public void updateSmsSent() {
		mSms_spinner.clearAnimation();
		mSms_spinner.setImageResource(R.drawable.tick);
		mDone.setVisibility(View.VISIBLE);
		this.setCancelable(true);
	}
	
	public void updateSmsFailure(String failure) {
		mSms.setText(failure);
		mSms_spinner.clearAnimation();
		mSms_spinner.setImageResource(R.drawable.cross);
		mDone.setVisibility(View.VISIBLE);
		this.setCancelable(true);
	}
}
